﻿using System;
using System.Linq;
using System.Data;
using System.IO;
using TDT_Project.classes;
using System.Text;
using Telerik.Web.UI;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TDT_Project.Controller
{
    public partial class UpDeKT : System.Web.UI.UserControl
    {
        cls_nienkhoa nienche = new cls_nienkhoa();
        cls_lop lop = new cls_lop();
        cls_phuhuynhxemdiem cls = new cls_phuhuynhxemdiem();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                load_nienche();
                load_Khoi();
                load_mon();
                DataTable dt = new DataTable();
                rid_KT.DataSource = dt;
                rid_KT.DataBind();
                string path = "~/images/Upload/HinhAnh/";
                RadEditor1.ImageManager.ViewPaths= new string[]{path};
                RadEditor1.ImageManager.UploadPaths = new string[] { path };
                RadEditor1.ImageManager.DeletePaths = new string[] { path };
            }
        }

        protected void btn_them_Click(object sender, EventArgs e)
        {
            Refresh();
        }

        protected void btn_luu_Click(object sender, EventArgs e)
        {
            try
            {
                if (txttieude.Text == "")
                {
                    ScriptManager.RegisterStartupScript(Up1, Up1.GetType(), "script", "alert('Hãy nhập tiêu đề');", true);
                    return;
                }
                if (RadEditor1.Text=="")
                {
                    ScriptManager.RegisterStartupScript(Up1, Up1.GetType(), "script", "alert('Hãy nhập nội dung');", true);
                    return;
                }
                string nienhoc = cbo_nienhoc.SelectedValue.ToString();
                string monhoc = cbo_monhoc.SelectedValue.ToString();
                string khoi = cbo_Khoi.SelectedValue.ToString();
                string file = Upload();
                if (txtID.Text == "")
                {
                    bool i = cls.Insert_DAKT(nienhoc, khoi, monhoc, DateTime.Now, txttieude.Text, RadEditor1.Content, file);
                    if (i == false)
                    {
                        ScriptManager.RegisterStartupScript(Up1, Up1.GetType(), "script", "alert('Thêm không thành công');", true);
                        return;
                    }
                    DataSet ds = cls.Get_DAKT(nienhoc, khoi, monhoc);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        rid_KT.DataSource = ds.Tables[0].DefaultView;
                        rid_KT.DataBind();

                    }
                    txtID.Text = "";
                    txttieude.Text = "";
                    RadEditor1.Content = "";                    
                    cls_dungchung.fileURL = "";
                }
                else
                {
                    bool i=true;
                    if(file!="")
                         i = cls.Update_DAKT(nienhoc, khoi, monhoc, DateTime.Now, txttieude.Text, RadEditor1.Content, file,Convert.ToInt32(txtID.Text));
                    else
                        i = cls.Update_DAKT(nienhoc, khoi, monhoc, DateTime.Now, txttieude.Text, RadEditor1.Content, cls_dungchung.fileURL, Convert.ToInt32(txtID.Text));
                    if (i == false)
                    {
                        ScriptManager.RegisterStartupScript(Up1, Up1.GetType(), "script", "alert('Cập nhật không thành công');", true);
                        return;
                    }
                    DataSet ds = cls.Get_DAKT(nienhoc, khoi, monhoc);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        rid_KT.DataSource = ds.Tables[0].DefaultView;
                        rid_KT.DataBind();

                    }
                    txtID.Text = "";
                    txttieude.Text = "";
                    RadEditor1.Content = "";
                    cls_dungchung.fileURL = "";
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        protected void btn_tim_Click(object sender, EventArgs e)
        {
            string nienhoc = cbo_nienhoc.SelectedValue.ToString();
            string monhoc = cbo_monhoc.SelectedValue.ToString();
            string khoi = cbo_Khoi.SelectedValue.ToString();
            DataSet ds = cls.Get_DAKT(nienhoc, khoi, monhoc);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                rid_KT.DataSource = ds.Tables[0].DefaultView;
                rid_KT.DataBind();

            }
            else
            {
                DataTable dt = new DataTable();
                rid_KT.DataSource = dt;
                rid_KT.DataBind();
            }
        }
        private void Refresh()
        {
            Response.Redirect(Request.RawUrl);
        }
        private void load_nienche()
        {
            DataSet ds = nienche.load_nienkho_combo();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                cbo_nienhoc.DataSource = ds;
                cbo_nienhoc.DataTextField = "TenNienHoc";
                cbo_nienhoc.DataValueField = "NienHoc_id";
                cbo_nienhoc.DataBind();
                string s = "";
                if (DateTime.Now.Month > 8)
                    s = DateTime.Now.Year + "-" + DateTime.Now.AddYears(1).Year;
                else
                    s = DateTime.Now.AddYears(-1).Year + "-" + DateTime.Now.Year;
                DataRow[] dr = ds.Tables[0].Select("TenNienHoc='" + s + "'");
                if (dr.Count() > 0)
                    cbo_nienhoc.SelectedValue = dr[0][0].ToString();
            }
        }
        private void load_Khoi()
        {
            DataSet ds = cls.Get_Khoi();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                cbo_Khoi.DataSource = ds;
                cbo_Khoi.DataTextField = "TenKhoi";
                cbo_Khoi.DataValueField = "Khoi_Id";
                cbo_Khoi.DataBind();

            }
        }
        private void load_mon()
        {

            DataSet ds_mon = cls.Get_Mon();
            if (ds_mon != null && ds_mon.Tables[0].Rows.Count > 0)
            {
                cbo_monhoc.DataSource = ds_mon;
                cbo_monhoc.DataTextField = "TenMonHoc";
                cbo_monhoc.DataValueField = "MonHoc_id";
                cbo_monhoc.DataBind();
                cbo_monhoc.SelectedIndex =1;
            }
        }
        private string Upload()
        {
            string str = "";
            foreach (UploadedFile f in RadAsyncUpload1.UploadedFiles)
            {
                str = DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "")+"_"+f.FileName ;
                f.SaveAs(Server.MapPath("/file/") + str);
                str = "/file/" + str;
                str="<a href='"+str+"'style='color:blue'><u>Tải về</u></a>";
            }
            return str;

        }

        protected void rid_KT_ItemCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                string cmd = e.CommandName;
                int iRow = e.Item.ItemIndex;
                if (cmd == "EditItem")
                {
                    txtID.Text=rid_KT.Items[iRow]["ID"].Text;
                    txttieude.Text = rid_KT.Items[iRow]["TieuDe"].Text;
                    RadEditor1.Content = rid_KT.Items[iRow]["NoiDung"].Text;
                    cbo_Khoi.SelectedValue = rid_KT.Items[iRow]["Khoi_ID"].Text;
                    cbo_nienhoc.SelectedValue = rid_KT.Items[iRow]["NienHoc_ID"].Text;
                    cbo_monhoc.SelectedValue = rid_KT.Items[iRow]["Mon_ID"].Text;
                    cls_dungchung.fileURL = rid_KT.Items[iRow]["FileURL"].Text;
                }
                else 
                {
                    string nienhoc = cbo_nienhoc.SelectedValue.ToString();
                    string monhoc = cbo_monhoc.SelectedValue.ToString();
                    string khoi = cbo_Khoi.SelectedValue.ToString();
                    bool i = cls.Xoa_DAKT(Convert.ToInt32(txtID.Text));
                    if (i == false)
                    {
                        ScriptManager.RegisterStartupScript(Up1, Up1.GetType(), "script", "alert('Xóa không thành công');", true);
                        return;
                    }
                    DataSet ds = cls.Get_DAKT(nienhoc, khoi, monhoc);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        rid_KT.DataSource = ds.Tables[0].DefaultView;
                        rid_KT.DataBind();

                    }
                    else
                    {
                         DataTable dt = new DataTable();
                        rid_KT.DataSource = dt;
                        rid_KT.DataBind();
                    }
                }
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }
}